package com.cyj.crm02.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.crm02.entity.Customer;
import com.cyj.crm02.entity.Employee;
import com.cyj.crm02.pojo.CustomerVo;
import com.cyj.crm02.service.CustomerService;
import com.cyj.crm02.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     *  查询一页客户信息
     *
     */
    @GetMapping("/pageList")
    public Map getList(Integer page, Integer limit ){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<CustomerVo> page1 = new Page<CustomerVo>();
        page1.setSize(limit);
        page1.setCurrent(page);

        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();

        int employeeId = employee.getEmployeeId();
        System.out.println(employeeId);

        IPage<CustomerVo> iPage = customerService.selectCustomerList(page1,employeeId);

        map.put("msg","查询情况");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);

        System.out.println(map);
        return map;
    }

    /**
     *  删除客户
     */
    @DeleteMapping("/del/{customerId}")
    public Map del(@PathVariable Integer customerId){
        Customer customer = new Customer();
        customer.setCustomerId(customerId);
        customer.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        int num = 0;
        boolean flag = customerService.updateById(customer);
        if (flag==true){
            num = 1;
            result.put("msg","删除成功");
        }else {
            result.put("msg","删除失败");
        }
        result.put("state",num);
        return result;
    }

    /**
     *  新增客户
     */
    @PostMapping(value="/add",produces = "application/json;charset=UTF-8")
    public Map add(Customer customer){
        customer.setCreateTime(StringUtils.getNowTime());
        customer.setUpdateTime(StringUtils.getNowTime());
        System.out.println("添加的参数customer"+customer);
        Map<String,Object> result = new HashMap<String,Object>();
        int num = 0;

        boolean flag = customerService.save(customer);
        if(flag==true){
            num = 1;
        }

        result.put("state",num);
        if(num ==1){
            result.put("msg","添加成功");
        }else {
            result.put("msg","添加失败");
        }
        return result;
    }

    /**
     *  修改客户
     */
    @PutMapping("/update")
    public Map edit(Customer customer){
        System.out.println("customer:"+customer);
        customer.setUpdateTime(StringUtils.getNowTime());
        Map<String,Object> result = new HashMap<String,Object>();
        boolean flag = customerService.updateById(customer);
        System.out.println(flag);
        int num = 0;
        if (flag == true){
            num = 1;
        }

        result.put("state",num);
        return result;
    }
}
